package com.qingke.memo.factory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.memo.command.Command;
import com.qingke.memo.command.memo.AlterMemoCustomTag;
import com.qingke.memo.command.memo.CreateMemoCustomTag;
import com.qingke.memo.command.memo.DeleteMemoCustomTag;
import com.qingke.memo.command.memo.HelpMemo;
import com.qingke.memo.command.memo.LeaveMemo;
import com.qingke.memo.pojo.Memo;

public class MemoCommandFactory extends SystemCommandFactory {
	private static Map<CommandCode, Class<? extends Command>> memoMap = new HashMap<>();

	static {
		memoMap.put(CommandCode.ALTERMEMOCUSTOMTAG, AlterMemoCustomTag.class);
		memoMap.put(CommandCode.CREATEMEMOCUSTOMTAG, CreateMemoCustomTag.class);
		memoMap.put(CommandCode.DELETEMEMOCUSTOMTAG, DeleteMemoCustomTag.class);
		memoMap.put(CommandCode.HELP, HelpMemo.class);
		memoMap.put(CommandCode.LEAVEMEMO, LeaveMemo.class);
	}

	private Memo memo;

	public MemoCommandFactory(Memo memo) {
		this.memo = memo;
	}

	public Memo getMemo() {
		return memo;
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<? extends Command> commandClass = null;
		commandClass = memoMap.get(cmdCode);
		if (commandClass == null)
			return super.buildCommand(cmdCode);

		Command command = null;
		try {
			command = commandClass.getConstructor(Memo.class).newInstance(this.getMemo());
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return command;
	}
}
